package com.lightboat.web.controller.system;

import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.utils.poi.ExcelUtil;
import com.lightboat.system.domain.SysUserHierarchy;
import com.lightboat.system.service.ISysUserHierarchyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户上下级关系Controller
 * 
 * @author lightboat
 * @date 2025-08-14
 */
@Tag(name = "用户上下级关系")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/hierarchy")
public class SysUserHierarchyController extends BaseController
{

    private final ISysUserHierarchyService sysUserHierarchyService;

    /**
     * 查询用户上下级关系列表
     */
    @Operation(summary = "查询用户上下级关系列表")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserHierarchy sysUserHierarchy)
    {
        startPage();
        List<SysUserHierarchy> list = sysUserHierarchyService.selectSysUserHierarchyList(sysUserHierarchy);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 导出用户上下级关系列表
     */
    @Operation(summary = "导出用户上下级关系列表")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:export')")
    @Log(title = "用户上下级关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserHierarchy sysUserHierarchy)
    {
        List<SysUserHierarchy> list = sysUserHierarchyService.selectSysUserHierarchyList(sysUserHierarchy);
        ExcelUtil<SysUserHierarchy> util = new ExcelUtil<SysUserHierarchy>(SysUserHierarchy.class);
        util.exportExcel(response, list, "用户上下级关系数据");
    }

    /**
     * 获取用户上下级关系详细信息
     */
    @Operation(summary = "获取用户上下级关系详细信息")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserHierarchyService.selectSysUserHierarchyById(id));
    }

    /**
     * 新增用户上下级关系
     */
    @Operation(summary = "新增用户上下级关系")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:add')")
    @Log(title = "用户上下级关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserHierarchy sysUserHierarchy)
    {
        return toAjax(sysUserHierarchyService.insertSysUserHierarchy(sysUserHierarchy));
    }

    /**
     * 修改用户上下级关系
     */
    @Operation(summary = "修改用户上下级关系")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:edit')")
    @Log(title = "用户上下级关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserHierarchy sysUserHierarchy)
    {
        return toAjax(sysUserHierarchyService.updateSysUserHierarchy(sysUserHierarchy));
    }

    /**
     * 删除用户上下级关系
     */
    @Operation(summary = "删除用户上下级关系")
    @PreAuthorize("@ss.hasPermi('system:hierarchy:remove')")
    @Log(title = "用户上下级关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserHierarchyService.deleteSysUserHierarchyByIds(ids));
    }
}
